package com.clkj.nangang.mvp.register;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.clkj.nangang.R;
import com.clkj.nangang.app.CustomApplication;
import com.clkj.nangang.mvp.base.BaseActivity;
import com.clkj.nangang.mvp.quarters.QuartersActivity;
import com.clkj.nangang.util.CommonUtil;
import com.clkj.nangang.util.schedulers.SchedulerProvider;

public class RegisterActivity extends BaseActivity implements RegistContract.View, View.OnClickListener {

    private EditText registEditName;
    private EditText registEditTel;
    private EditText registEditPwd;
    private EditText registEditPwded;
    private EditText xiaoquEdit;
    private EditText streetEdit;
    private EditText paihaoEdit;
    private Button btRegist;

    private RegistContract.Presenter presenter;
    private RadioGroup rgType;
    private RadioButton rbXiaoqu;
    private RadioButton rbZu;

    private String UrbanRural;

    private int REQUEST_QUARTER = 0x11;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initData();
        initView();
        assignView();
    }

    @Override
    public void initData() {
        presenter = new RegistPresenter(this, SchedulerProvider.getInstance(), CustomApplication.getInstance().getHttpService());

        UrbanRural = "小区";
    }

    @Override
    public void assignView() {
        xiaoquEdit.setOnClickListener(this);
        btRegist.setOnClickListener(this);

        rgType.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId) {
                    case R.id.rb_xiaoqu:
                        UrbanRural = "小区";
                        break;

                    case R.id.rb_zu:
                        UrbanRural = "组";
                        break;
                }
            }
        });
    }

    @Override
    public void initView() {

        registEditName = (EditText) findViewById(R.id.regist_edit_name);
        registEditTel = (EditText) findViewById(R.id.regist_edit_tel);
        registEditPwd = (EditText) findViewById(R.id.regist_edit_pwd);
        registEditPwded = (EditText) findViewById(R.id.regist_edit_pwded);
        xiaoquEdit = (EditText) findViewById(R.id.xiaoquEdit);
        streetEdit = (EditText) findViewById(R.id.streetEdit);
        paihaoEdit = (EditText) findViewById(R.id.paihaoEdit);
        btRegist = (Button) findViewById(R.id.bt_regist);
        rgType = (RadioGroup) findViewById(R.id.rg_type);
        rbXiaoqu = (RadioButton) findViewById(R.id.rb_xiaoqu);
        rbZu = (RadioButton) findViewById(R.id.rb_zu);
    }

    @Override
    public void onNetError(String tip) {
        Toast.makeText(this, tip, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void RegisterSuccess(String tip) {
        Toast.makeText(this, tip, Toast.LENGTH_SHORT).show();
        finish();
    }

    @Override
    public void RegisterFail(String tip) {
        Toast.makeText(this, tip, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void setPresenter(RegistContract.Presenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //选择小区或者组
            case R.id.xiaoquEdit:
                Intent intent = new Intent(RegisterActivity.this, QuartersActivity.class);
                intent.putExtra("type", UrbanRural);
                startActivityForResult(intent, REQUEST_QUARTER);
                break;
            //注册
            case R.id.bt_regist:
                String name = registEditName.getText().toString();
                String phone = registEditTel.getText().toString();
                String pass = registEditPwd.getText().toString();
                String repass = registEditPwded.getText().toString();

                String xiaoquName = xiaoquEdit.getText().toString();
                String street = streetEdit.getText().toString();
                String room = paihaoEdit.getText().toString();

                if (TextUtils.isEmpty(name)) {
                    Toast.makeText(RegisterActivity.this, "请输入姓名", Toast.LENGTH_SHORT).show();
                    registEditName.requestFocus();
                    return;
                }
                if (TextUtils.isEmpty(phone)) {
                    Toast.makeText(RegisterActivity.this, "请输入电话", Toast.LENGTH_SHORT).show();
                    registEditTel.requestFocus();
                    return;
                }
                if (TextUtils.isEmpty(pass)) {
                    Toast.makeText(RegisterActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                    registEditPwd.requestFocus();
                    return;
                }
                if (TextUtils.isEmpty(repass)) {
                    Toast.makeText(RegisterActivity.this, "请重复密码", Toast.LENGTH_SHORT).show();
                    registEditPwded.requestFocus();
                    return;
                }
                if (TextUtils.isEmpty(xiaoquName)) {
                    Toast.makeText(RegisterActivity.this, "请选择小区/组", Toast.LENGTH_SHORT).show();
                    xiaoquEdit.requestFocus();
                    return;
                }
                if (TextUtils.isEmpty(street)) {
                    Toast.makeText(RegisterActivity.this, "请输入楼/路", Toast.LENGTH_SHORT).show();
                    streetEdit.requestFocus();
                    return;
                }
                if (TextUtils.isEmpty(room)) {
                    Toast.makeText(RegisterActivity.this, "请输入楼/路", Toast.LENGTH_SHORT).show();
                    paihaoEdit.requestFocus();
                    return;
                }
                if (!CommonUtil.isMobile(phone)) {
                    Toast.makeText(RegisterActivity.this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
                    paihaoEdit.requestFocus();
                    return;
                }
                if (!TextUtils.equals(pass, repass)) {
                    Toast.makeText(RegisterActivity.this, "两次数的密码不一致", Toast.LENGTH_SHORT).show();
                    paihaoEdit.requestFocus();
                }

                presenter.userRegist(name, pass, phone, UrbanRural, xiaoquName, street, room);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_QUARTER && resultCode == RESULT_OK && data != null) {
            //填充小区或组
            String xiaoqu = data.getStringExtra("xiaoqu");
            xiaoquEdit.setText(xiaoqu);
        }
    }
}
